// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:junyeyishu/widgets/QGWidget.dart';
import 'package:permission_handler/permission_handler.dart';

class QGWebView extends StatefulWidget {
  String? url;
  QGWebView({super.key, this.url});
  @override
  State<QGWebView> createState() => _QGWebViewState();
}

class _QGWebViewState extends State<QGWebView> {
  String? title;
  bool loading = true;

  @override
  Widget build(BuildContext context) {
    return QGScaffold(
      title: title ?? "",
      body: InAppWebView(
          initialUrlRequest: URLRequest(url: WebUri(widget.url ?? "")),
          // initialUrlRequest: URLRequest(url: Uri.parse(widget.url ?? "")),
          onWebViewCreated: (controller) {},
          onLoadStop: (controller, url) async {
            title = await controller.getTitle();
            setState(() {});
          },
          // initialOptions: InAppWebViewGroupOptions(
          //   crossPlatform: InAppWebViewOptions(
          //     supportZoom: false,
          //     javaScriptEnabled: true,
          //     disableHorizontalScroll: true,
          //     disableVerticalScroll: true,
          //   ),
          // ),
          // androidOnPermissionRequest: (InAppWebViewController controller,
          //     String origin, List<String> resources) async {
          //   return PermissionRequestResponse(
          //       resources: resources,
          //       action: PermissionRequestResponseAction.GRANT);
          // }),
          initialSettings: InAppWebViewSettings(
            supportZoom: false,
            javaScriptEnabled: true,
            disableHorizontalScroll: true,
            disableVerticalScroll: true,
          ),
          onPermissionRequest: (controller, permissionRequest) async {
            if (!kIsWeb) {
            if (Platform.isAndroid) {
              return PermissionResponse(action: PermissionResponseAction.GRANT);
            }
            }
            return PermissionResponse(action: PermissionResponseAction.DENY);
          }
          ),
    );
  }
}
